<?php

namespace App\Http\Resources;

use App\Enums\OpenShop\InvestmentPromotion\UploadFileResourceEnum;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\MissingValue;

class BaseResource extends JsonResource
{

    /**
     * @param string $rel
     * @return bool
     */
    public function resourceLoaded(string $rel): bool
    {
        if ($this->whenLoaded($rel) instanceof MissingValue) {
            return false;
        }
        return true;
    }

//    /**
//     * @param array $attachMap
//     * @return array
//     */
//    public function getAttachFileInfo(array $attachMap): array
//    {
//        return collect($attachMap)->map(function ($v, $k) {
//            return [
//                "key" => $v,
//                "url" => $this->attachRel
//                    ->where("position", $k)
//                    ->pluck("url")
//                    ->map("privateFilePath")
//                    ->values()
//                    ->toArray()
//            ];
//        })->values()->pluck("url", "key")->toArray();
//    }

    public function getAttachFileInfo(array $attachMap): array
    {
        return collect($attachMap)->map(function ($v, $k) {
            return [
                "key" => $v,
                "url" => $this->attachRel->where("position", $k)->map(function ($v) {
                    if ($v->isPublic == 1) {
                        $v->url = picturePath($v->url);
                    } else {
                        $v->url = privateFilePath($v->url);
                    }
                    return $v;
                })->pluck("url")->values()->toArray()
            ];
        })->values()->pluck("url", "key")->toArray();
    }

}
